<?php
namespace app\web\controller;

use app\model\Record; 
use app\model\Theme; 

class Check extends Base
{
    public function Check()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '参团列表 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
$id = intval($_P['id']);
$do = 'check';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid));
$themelist = Theme::getAllTheme();
if(($config['ishexiao']==0)){
	return_msg('核销功能未开启，请在：活动编辑->其它设置->商家核销->开启');
}

if($op == 'hexiao'){
	$themeid = intval($_P['themeid']);
	$uid = intval($_P['uid']);
	$shopid = intval($_P['shopid']);
	$shop=db_find("SELECT * FROM ".tablename(t_hexiao)." WHERE uid = '{$uid}' and shopid = '{$shopid}' and themeid = '{$themeid}'");
	if(!empty($shop) ){
		if ($shop['workerid']==-1){
			$hexiaoworker = array();
			$hexiaoworker['username']='后台';
		}else{
			$hexiaoworker=db_find("SELECT * FROM ".tablename(t_worker)." WHERE uid = '{$shop['workerid']}'");
		}
		return_msg('错误:已经于'.$shop['addtime'].'由本公司'.$hexiaoworker['username'].'核销过','', 'error') ;
	}else{
		$data ['uniacid'] = $_G['mpid'];
		$data ['uid'] = $uid;
		$data ['shopid'] = $shopid;
		$data ['themeid'] = $themeid;
		$data ['workerid'] = -1;
		$data ['addtime'] = date('Y-m-d H:i:s');
		db_insert ( t_hexiao, $data );
		return_tips('后台手工核销成功', $url, 'info');
	}
}
if($op == 'display'){

	$sql = 'SELECT w.* FROM ' . tablename(t_shangjia) . ' as w
			WHERE  w.state=1 ORDER BY w.displayorder';
	$shoplist = db_select($sql);
	
	$shoplist1 = $shoplist;
	$shoplist2 = $shoplist;

	$gsumall = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE themeid ='{$themeid}'  and status > 0 " ));

	foreach ($shoplist as $index =>$row){
		$gsum = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_hexiao ) . "
				WHERE themeid ='{$themeid}' and shopid ='{$row['id']}' " ));
		$shoplist1[$index]['num'] = $gsumall - $gsum;
		$shoplist2[$index]['num'] = $gsum;
	}


	$sql = 'SELECT w.*,t.name as teamname,s.name as companyname FROM ' . tablename(t_worker) . " as w 
			left join " . tablename ( t_team ) . " as t on w.team = t.id
			left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
			WHERE  w.themeid = :themeid
			ORDER BY company,team,uid  ";
	$wlist = db_select($sql, array(":themeid"=>$themeid));
	
	
	$pageNumber = max ( 1, intval ( $_P['page'] ) );
    if ($theme['endtime']<strtotime("-90 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben') {
            return_msg('活动结束超3个月,无法查看');
        }
        $pageSize = 20;
    }elseif ($theme['endtime']<strtotime("-30 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben' && $_G['username']!=='caiwu' && $_G['username']!=='chuna') {
            return_msg('活动结束超1个月,无法查看');
        }
        $pageSize = 20;
    }else{
		$pageSize = 50;
	}

	if (empty($_P['createtime']['start'])){
		$starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
		$starttime =  strtotime(date('Y-m-d 00:00:00',$starttime));
	}else{
		$starttime =  strtotime($_P['createtime']['start']);
	}
    $endtime = empty($_P['createtime']['end']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['createtime']['end']);
    

	$condition = " and 1=1 "; 

	$condition.= " and ord.addtime > '{$starttime}' and ord.addtime <= '{$endtime}' ";
    
	
	if($_P['op2'] == 'scan'){
		$condition.= " and (ord.pid = '{$id}' or ord.id = '{$id}') ";
	}
	
	$condition .= " and ord.themeid = '{$themeid}' ";
	
	$workerid = $_P['workerid'];
	if(!empty($workerid)){
		$condition.= " and ord.workerid = '{$workerid}'  ";
	}
	if(!empty($_P['zhijie'])){
		$condition.= " and ord.fromuid = '{$workerid}'  ";
	}
	
	if(!isset($_P['type'])){
		$type = -1;
	}else{
		$type = intval($_P['type']);
	}


	if($type == 10){
		$condition.= " and ord.isfinish = '0' and ord.status >0 ";
	}elseif($type == 11){
		$condition.= " and ord.isfinish = '1' and ord.status >0 ";
	}elseif($type >= 0){
		$condition.= " and ord.type = '{$type}'  ";
	}

	if($type > 9){
		$sorttype = ' ORDER BY ord.pid desc, ord.type asc, ord.addtime asc';
	}else{
		$sorttype = ' ORDER BY ord.addtime desc ';
	}


	$condition.= " and ord.status > 0  ";

	
    $keyword = trim($_P['keyword']);
	if(!empty($keyword)){
		$condition.= " and (ord.nickname like '%{$keyword}%' or ord.username like '%{$keyword}%' or ord.mobile like '%{$keyword}%' )";
	}
	$sql =  "SELECT COUNT(ord.id) FROM " . tablename ( t_record ) . " as ord
			WHERE  1=1  {$condition} ORDER BY ord.id desc";
	$total = db_fetchcolumn ($sql,$params);
	
	
	if($_P['op2']=='export_submit'){

	  //   $con2 = "ord.uniacid = '{$_G['mpid']}' and ord.themeid = '{$themeid}' and ord.type = 0";
   //      if($status >= 0){
   //          $con2.= " and ord.status = '{$status}'  ";
   //      }
   //      $sql =  "SELECT COUNT(ord.id) FROM " . tablename ( t_record ) . " as ord
			// WHERE {$con2}  ORDER BY ord.id desc";
   //      $count = db_fetchcolumn ($sql,$params);
        $count = $total;
		$pagesize = ceil($count/5000);
		// $header = array('id'=>'ID', 'nickname' => '微信昵称',
  //           'username' => '姓名', 'mobile' => '手机','address' =>$config['address'],
		//     'option' => $config['shops'],'datetime'=>$config['datetime'],
		// 	'fee' => '费用',  'nickname2' => '推荐', 'status' => '状态','tuanid' => '团号','type' => '身份','isfinish' => '拼团状态',	'addtime'=>'时间'
		// );
		$header = array();
		$header['id'] = '定单号';
		$header['nickname'] = '微信昵称';
		$header['username'] = '姓名';
		$header['mobile'] = '手机';
		if (!empty($config['address'])) {
			$header['address'] = $config['address'];
		}
		if (!empty($config['shops'])) {
			$header['option'] = $config['shops'];
		}
		if (!empty($config['datetime'])) {
			$header['datetime']= $config['datetime'];
		}
			
		$header['fee'] = '金额';
		$header['nickname2'] = '推荐';
		$header['status'] = '状态';
		if ($system['ruanjian']!==1) {
			$header['tuanid'] = '团号';
			$header['type'] = '身份';
			$header['isfinish'] = '拼团状态';
		}
		$header['addtime'] = '时间';
		if ($system['ruanjian']==3) {
			$header['companyname']='公司';
		}
		$header['teamname']='团队';
		$header['workername']='员工';

		if (empty($shoplist)) {
			$header['check']='核销';
		}else{
			foreach ($shoplist as $shopindex =>$shoprow){
				$header['check'.$shopindex]=mb_substr($shoprow['name'],0,6,'utf-8');
			}
		}
		$keys = array_keys($header);
		$html = "\xEF\xBB\xBF";
		foreach ($header as $li) {
			$html .= $li . "\t ,";
		}
		$html .= "\n";
		$statustr = array("未支付","已支付","已核销","线下定单","已退款","虚拟定单");
		$luckstr = array("否","是");
        $typestr = array("团长","团员","单买");
        $groupstr = array("拼团中","拼团成功");
		for ($j = 1; $j <= $pagesize; $j++) {
			$sql = "SELECT ord.* FROM " . tablename ( t_record ) . " as ord
						WHERE  1=1 {$condition} 
						  {$sorttype} LIMIT " . ($j - 1) * 50000 . ",50000 ";
			$list = db_select($sql, $params);
			$array = array();
			foreach ($list as $index =>$row){
				if(!empty($row['fromuid'])){
					$fans = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " 
					            WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['fromuid']));
					$list[$index]['nickname2'] = $fans['nickname'];
					$list[$index]['avatar2'] = $fans['avatar'];
				}
				if(!empty($row['workerid'])){
					$fans = db_find ( "SELECT * FROM " . tablename ( t_worker ) . " WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['workerid']));
					$list[$index]['workername'] = $fans['username'];
					if (empty($fans['username'])) {
						$list[$index]['workername'] = $row['workerid'];
					}
				}
				if(!empty($row['workerid'])){
					$fans = db_find ( "SELECT w.username,t.name as teamname,s.name as companyname FROM " . tablename ( t_worker ) . " as w
						left join " . tablename ( t_team ) . " as t on w.team = t.id
						left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
						WHERE w.uid = :uid and w.themeid =:themeid
						",array(":themeid"=>$themeid,":uid"=>$row['workerid']));
					$list[$index]['workername'] = $fans['username'];
					$list[$index]['teamname'] = $fans['teamname'];
					$list[$index]['companyname'] = $fans['companyname'];
					if (empty($fans['username'])) {
						$list[$index]['workername'] = $row['workerid'];
					}
				}

				if ($row['type']==0){
					$list[$index]['tuanid'] = $row['id']+10000;
				}elseif ($row['type']==1){
					$list[$index]['tuanid'] = $row['pid']+10000;
				}elseif ($row['type']==9){
					$list[$index]['tuanid'] = 0;
				}
				$list[$index]['nickname']=str_replace('"',"",$list[$index]['nickname']);
				$list[$index]['nickname']=str_replace(',',"",$list[$index]['nickname']);
				$list[$index]['nickname2']=str_replace('"',"",$list[$index]['nickname2']);
				$list[$index]['nickname2']=str_replace(',',"",$list[$index]['nickname2']);
				if (empty($list[$index]['nickname2'])){
					$list[$index]['nickname2']=$row['fromuid'];
				}
				$list[$index]['username']=str_replace('"',"",$list[$index]['username']);
				$list[$index]['username']=str_replace(',',"",$list[$index]['username']);


				if (empty($shoplist)) {
					$sql='SELECT h.workerid,h.addtime as hxtime,w.username as workername FROM ' . tablename ( t_hexiao ) . " as h
							left join " . tablename ( t_worker ) . " as w on w.uid = h.workerid
							WHERE h.themeid ='{$themeid}' and h.shopid ='0' and h.uid ='{$row['uid']}'";
					$hexiao = db_find ( $sql );
					if ($hexiao['workerid']==-1){
						$hexiao['workername']='后台';
					}
					if (!empty($hexiao['hxtime'])){
						$list[$index]['check'] = $hexiao['workername'].":".$hexiao['hxtime'];
					}else{
						$list[$index]['check'] = "未核销";
					}
				}else{
					foreach ($shoplist as $shopindex =>$shoprow){
						$sql='SELECT h.workerid,h.addtime as hxtime,w.username as workername FROM ' . tablename ( t_hexiao ) . " as h
								left join " . tablename ( t_worker ) . " as w on w.uid = h.workerid
								WHERE h.themeid ='{$themeid}' and h.shopid ='{$shoprow['id']}' and h.uid ='{$row['uid']}'";
						$hexiao = db_find ( $sql );
						if ($hexiao['workerid']==-1){
							$hexiao['workername']='后台';
						}
						if (!empty($hexiao['hxtime'])){
							$list[$index]['check'.$shopindex] = $hexiao['workername'].":".$hexiao['hxtime'];
						}else{
							$list[$index]['check'.$shopindex] = "未核销";
						}
					}
				}


                $array[] = $list[$index];
                // $details = Record::getAllDetailListByPidAndStatus($row['id'],$status);
                // foreach ($details as $index2 =>$row2){
                //     $fans = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " 
					           //  WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row2['fromuid']));
                //     $details[$index2]['nickname2'] = $fans['nickname'];
                //     $details[$index2]['avatar2'] = $fans['avatar'];
                //     $array[] = $details[$index2];
                // }
                //$array[] = array();
			}


			if (!empty($array)) {
				// $size = ceil(count($array) / 1000);
				// for ($i = 0; $i < $size; $i++) {
				// 	$buffer = array_slice($array, $i * 1000, 1000);
					foreach ($array as $row) {

                        if($row['addtime'] > 0){
                            $row['addtime']  = date('Y-m-d H:i:s',$row['addtime']);
                        }
                        if($row['status'] > 0){
                            $row['status'] = $statustr[$row['status']];
                            $row['isfinish'] = $groupstr[$row['isfinish']]; 
                        }elseif( $row['status'] == -1){
                            $row['status'] = $statustr[4];
                            $row['isfinish'] = ""; 
                        }elseif( $row['status'] == -2){
                            $row['status'] = $statustr[5];
                            $row['isfinish'] = $groupstr[$row['isfinish']]; 
                        }else{
                            $row['status'] = $statustr[$row['status']];
                            if ($row['status'] == 0) {
                            	$row['isfinish'] = ""; 
                            }else{
                            	$row['isfinish'] = $groupstr[$row['isfinish']]; 
                            }
                        }
                        if ($row['type']==9){$row['type']=2;}
                        $row['type'] = $typestr[$row['type']];
                        $row['id'] = $row['id']+9000;
						unset($info);
						foreach ($keys as $key) {
							$data[] = $row[$key];
						}
						$user[] = implode("\t ,", $data) . "\t ,";
						unset($data);
					}
					$html .= implode("\n", $user);
				// }
			}
		}
        $r = writelogcz($system,$themeid,'导出数据',count($list) ."条核销总表导出",$_G['username']);
        if ($r) {
			header("Content-type:text/csv");
			header("Content-Disposition:attachment; filename=核销数据.csv");
			echo $html;
        }
		exit();
	}
	
	$sql = "SELECT ord.* FROM " . tablename ( t_record ) . " as ord
						WHERE  1=1 {$condition} 
						      {$sorttype}  LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;

	$list = db_select ( $sql,$params);

	foreach ($list as $index =>$row){
		if (empty($shoplist)) {
				$sql='SELECT h.workerid,h.addtime as hxtime,w.username as workername FROM ' . tablename ( t_hexiao ) . " as h
						left join " . tablename ( t_worker ) . " as w on w.uid = h.workerid
						WHERE  h.themeid ='{$themeid}' and h.shopid ='0' and h.uid ='{$row['uid']}'";
				$hexiao = db_find ( $sql );
				if ($hexiao['workerid']==-1){
					$hexiao['workername']='后台';
				}
				$list[$index]['shop'.$shopindex] = $hexiao;
			
				$gsum = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_hexiao ) . "
				WHERE themeid ='{$themeid}' and shopid ='0' " ));
		}else{
			foreach ($shoplist as $shopindex =>$shoprow){
				$sql='SELECT h.workerid,h.addtime as hxtime,w.username as workername FROM ' . tablename ( t_hexiao ) . " as h
						left join " . tablename ( t_worker ) . " as w on w.uid = h.workerid
						WHERE h.themeid ='{$themeid}' and h.shopid ='{$shoprow['id']}' and h.uid ='{$row['uid']}'";
				$hexiao = db_find ( $sql );
				if ($hexiao['workerid']==-1){
					$hexiao['workername']='后台';
				}
				$list[$index]['shop'.$shopindex] = $hexiao;
			}
		}

	}
	//exit;
	$pager = pagination ( $total, $pageNumber, $pageSize );
}

if($op == 'mingxi'){
	$pageNumber = max ( 1, intval ( $_P['page'] ) );
    if ($theme['endtime']<strtotime("-90 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben') {
            return_msg('活动结束超3个月,无法查看');
        }
        $pageSize = 20;
    }elseif ($theme['endtime']<strtotime("-30 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben' && $_G['username']!=='caiwu' && $_G['username']!=='chuna') {
            return_msg('活动结束超1个月,无法查看');
        }
        $pageSize = 20;
    }else{
		$pageSize = 50;
	}

	$sql = 'SELECT w.*,t.name as teamname,s.name as companyname FROM ' . tablename(t_worker) . " as w 
			left join " . tablename ( t_team ) . " as t on w.team = t.id
			left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
			WHERE  w.themeid = :themeid
			ORDER BY company,team,uid  ";
	$wlist = db_select($sql, array(":themeid"=>$themeid));
	
	$sql = 'SELECT w.* FROM ' . tablename(t_shangjia) . ' as w
			WHERE  w.state=1 ORDER BY w.displayorder';
	$shoplist = db_select($sql);

	if (empty($_P['createtime']['start'])){
		$starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
		$starttime =  date('Y-m-d 00:00:00',$starttime);
	}else{
		$starttime =  $_P['createtime']['start'];
	}
    $endtime = empty($_P['createtime']['end']) ? date('Y-m-d 23:59:59',TIMESTAMP): $_P['createtime']['end'];
    

	$condition = " and h.addtime > '{$starttime}' and h.addtime <= '{$endtime}' ";
    $starttime = strtotime($starttime);
    $endtime = strtotime($endtime);

	$workerid = $_P['workerid'];
	if(!empty($workerid)){
		$condition.= " and r.workerid = '{$workerid}'  ";
	}

	$shopid = $_P['shopid'];
	if(!empty($shopid)){
		$condition.= " and h.shopid = '{$shopid}'  ";
	}

    $keyword = trim($_P['keyword']);
	if(!empty($keyword)){
		$condition.= " and (r.nickname like '%{$keyword}%' or r.username like '%{$keyword}%' or r.mobile like '%{$keyword}%' )";
	}

	$sql =  "SELECT count(h.id) FROM " . tablename ( t_hexiao ) . " as h
		left join " . tablename ( t_record ) . " as r on h.uid =r.uid and h.themeid = r.themeid
						WHERE  1=1 {$condition}";
	$total = db_fetchcolumn ($sql,$params);
	
	if(checksubmit('export_submit', true)){
        $count = $total;
		$pagesize = ceil($count/5000);
		$header = array();
		$header['id'] = '定单号';
		$header['nickname'] = '微信昵称';
		$header['username'] = '姓名';
		$header['mobile'] = '手机';
		if (!empty($shoplist)) {
			$header['companyname'] = '核销公司';
		}
		$header['teamname'] = '核销团队';
		$header['workername'] = '核销人';
		$header['hexiaotime'] = '核销时间';

		$keys = array_keys($header);
		$html = "\xEF\xBB\xBF";
		foreach ($header as $li) {
			$html .= $li . "\t ,";
		}
		$html .= "\n";

		for ($j = 1; $j <= $pagesize; $j++) {
			$sql = "SELECT *,h.workerid as hexiaoworker,h.addtime as hexiaotime FROM " . tablename ( t_hexiao ) . " as h
		left join " . tablename ( t_record ) . " as r on h.uid =r.uid and h.themeid = r.themeid
						WHERE  1=1 {$condition} order by h.id desc  LIMIT " . ($j - 1) * 50000 . ",50000 ";
			$list = db_select($sql, $params);
			$array = array();
			foreach ($list as $index =>$row){

				$list[$index]['nickname']=str_replace('"',"",$list[$index]['nickname']);
				$list[$index]['nickname']=str_replace(',',"",$list[$index]['nickname']);
				$list[$index]['nickname2']=str_replace('"',"",$list[$index]['nickname2']);
				$list[$index]['nickname2']=str_replace(',',"",$list[$index]['nickname2']);
				if (empty($list[$index]['nickname2'])){
					$list[$index]['nickname2']=$row['fromuid'];
				}
				$list[$index]['username']=str_replace('"',"",$list[$index]['username']);
				$list[$index]['username']=str_replace(',',"",$list[$index]['username']);

				if($row['hexiaoworker']=='-1'){
					$list[$index]['workername'] = "后台";
					$sql = 'SELECT w.* FROM ' . tablename(t_shangjia) . ' as w
					WHERE  w.id=' . $row['shopid'] .'';
					$shop = db_find($sql);
					$list[$index]['companyname'] = $shop['name'];
				}elseif(!empty($row['hexiaoworker'])){
					$fans = db_find ( "SELECT w.username,t.name as teamname,s.name as companyname FROM " . tablename ( t_worker ) . " as w
						left join " . tablename ( t_team ) . " as t on w.team = t.id
						left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
						WHERE w.uid = :uid and w.themeid =:themeid
						",array(":themeid"=>$themeid,":uid"=>$row['hexiaoworker']));
					$list[$index]['workername'] = $fans['username'];
					$list[$index]['teamname'] = $fans['teamname'];
					$list[$index]['companyname'] = $fans['companyname'];
				}
				$array[] = $list[$index];
			}


			if (!empty($array)) {
				// $size = ceil(count($array) / 1000);
				// for ($i = 0; $i < $size; $i++) {
				// 	$buffer = array_slice($array, $i * 1000, 1000);
					foreach ($array as $row) {

                        if($row['addtime'] > 0){
                            $row['addtime']  = date('Y-m-d H:i:s',$row['addtime']);
                        }
                        if($row['status'] > 0){
                            $row['status'] = $statustr[$row['status']];
                            $row['isfinish'] = $groupstr[$row['isfinish']]; 
                        }elseif( $row['status'] == -1){
                            $row['status'] = $statustr[4];
                            $row['isfinish'] = ""; 
                        }elseif( $row['status'] == -2){
                            $row['status'] = $statustr[5];
                            $row['isfinish'] = $groupstr[$row['isfinish']]; 
                        }else{
                            $row['status'] = $statustr[$row['status']];
                            if ($row['status'] == 0) {
                            	$row['isfinish'] = ""; 
                            }else{
                            	$row['isfinish'] = $groupstr[$row['isfinish']]; 
                            }
                        }
                        if ($row['type']==9){$row['type']=2;}
                        $row['type'] = $typestr[$row['type']];
                        $row['id'] = $row['id']+9000;
						unset($info);
						foreach ($keys as $key) {
							$data[] = $row[$key];
						}
						$user[] = implode("\t ,", $data) . "\t ,";
						unset($data);
					}
					$html .= implode("\n", $user);
				// }
			}
		}

		$r = writelogcz($system,$themeid,'导出数据',count($list) ."条核销明细导出",$_G['username']);
        if ($r) {
			header("Content-type:text/csv");
			header("Content-Disposition:attachment; filename=核销数据明细.csv");
			echo $html;
        }
		exit();
	}

	$sql = "SELECT *,h.workerid as hexiaoworker,h.addtime as hexiaotime FROM " . tablename ( t_hexiao ) . " as h
		left join " . tablename ( t_record ) . " as r on h.uid =r.uid and h.themeid = r.themeid
						WHERE  1=1  {$condition} order by h.id desc  LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;

	$list = db_select ( $sql,$params);
	foreach ($list as $index =>$row){
		if($row['hexiaoworker']=='-1'){
			$list[$index]['workername'] = "后台";
			$sql = 'SELECT w.* FROM ' . tablename(t_shangjia) . ' as w
			WHERE  w.id=' . $row['shopid'] .'';
			$shop = db_find($sql);
			$list[$index]['companyname'] = $shop['name'];
		}elseif(!empty($row['hexiaoworker'])){
			$fans = db_find ( "SELECT w.username,t.name as teamname,s.name as companyname FROM " . tablename ( t_worker ) . " as w
				left join " . tablename ( t_team ) . " as t on w.team = t.id
				left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
				WHERE w.uid = :uid and w.themeid =:themeid
				",array(":themeid"=>$themeid,":uid"=>$row['hexiaoworker']));
			$list[$index]['workername'] = $fans['username'];
			$list[$index]['teamname'] = $fans['teamname'];
			$list[$index]['companyname'] = $fans['companyname'];
		}
	}
	//exit;
	$pager = pagination ( $total, $pageNumber, $pageSize );
}



$return_clicent = get_defined_vars();
return view('/check' , $return_clicent);
}
}